package com.sks.web.action.privilege;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.sks.bean.pojo.Users;
import com.sks.service.privilege.UsersService;
import com.sks.utils.MD5;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.privilege.UsersForm;
/***
 * 
 * 修改密码
 */
@Controller("/sks/privilege/users/changePwd")
public class ChangePaaawordAction extends BasicAction {
	@Resource UsersService usersService;
	
	/**
	 * 找回密码之修改密码
	 */
	public ActionForward changepassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm) form;
		String pwd = formbean.getLoginPwd().trim();
		String result="success";
		String redirUrl = "";
		if(StringUtils.hasText(pwd)){
					Users user = (Users) request.getSession().getAttribute("users");
					Calendar cd = Calendar.getInstance();
					user.setLastUpdateTime(cd.getTime());
					// update password
					
					user.setLoginPwd(MD5.MD5Encode(pwd));
					try{
						usersService.update(user);
						redirUrl =  "/sks/privilege/users/logout.do";
					}catch(Exception e){
						result = "failed";
					}
					
					
				}
					
		response.getWriter().write("{'result':'" + result + "'," + 
				                   "'redirUrl':'" + redirUrl+ "'}");
		return null;
		
		}
}
